package com.turman.builibrary.tab.bottom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.turman.builibrary.R;
import com.turman.builibrary.tab.common.IBTab;

/**
 * 底部导航栏item
 *
 * @author buobao
 * @since 2024/3/1 15:32
 */
public class BTabBottomItem extends RelativeLayout implements IBTab<BTabBottomInfo<?>> {
    private BTabBottomInfo<?> info;
    private ImageView tabImageView;
    private TextView tabIconView;
    private TextView tabNameView;

    public BTabBottomItem(Context context) {
        this(context, null);
    }

    public BTabBottomItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BTabBottomItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BTabBottomItem(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public BTabBottomInfo<?> getInfo() {
        return info;
    }

    @Override
    public void setBTabInfo(@NonNull BTabBottomInfo<?> data) {
        this.info = data;
        inflateInfo(false, true);
    }

    @Override
    public void resetHeight(int height) {
        ViewGroup.LayoutParams  layoutParams = getLayoutParams();
        layoutParams.height = height;
        setLayoutParams(layoutParams);
        getTabNameView().setVisibility(GONE);
    }

    @Override
    public void onTabSelectedChange(int index, @NonNull BTabBottomInfo<?> preInfo, @NonNull BTabBottomInfo<?> nextInfo) {
        if (preInfo != info && nextInfo != info || preInfo == nextInfo) {
            return;
        }
        if (preInfo == info) {
            inflateInfo(false, false);
        } else {
            inflateInfo(true, false);
        }
    }

    public ImageView getTabImageView() {
        return tabImageView;
    }

    public TextView getTabIconView() {
        return tabIconView;
    }

    public TextView getTabNameView() {
        return tabNameView;
    }


    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.b_tab_bottom, this);
        tabImageView = findViewById(R.id.iv_image);
        tabIconView = findViewById(R.id.tv_icon);
        tabNameView = findViewById(R.id.tv_name);
    }

    @SuppressLint("ResourceType")
    private void inflateInfo(boolean selected, boolean init) {
        if (info.tabType == BTabBottomInfo.TabType.ICON) {
            if (init) {
                tabImageView.setVisibility(GONE);
                tabIconView.setVisibility(VISIBLE);
                Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), info.iconFont);
                tabIconView.setTypeface(typeface);
                if (!TextUtils.isEmpty(info.name)) {
                    tabNameView.setText(info.name);
                }
            }
            if (selected) {
                tabIconView.setText(TextUtils.isEmpty(info.selectedIconName) ? info.defaultIconName : info.selectedIconName);
                int color = getColor(info.tintColor);
                tabIconView.setTextColor(color);
                tabNameView.setTextColor(color);
            } else {
                tabIconView.setText(info.defaultIconName);
                int color = getColor(info.defaultColor);
                tabIconView.setTextColor(color);
                tabNameView.setTextColor(color);
            }
        } else if (info.tabType == BTabBottomInfo.TabType.BITMAP) {
            if (init) {
                tabImageView.setVisibility(VISIBLE);
                tabIconView.setVisibility(GONE);
                if (!TextUtils.isEmpty(info.name)) {
                    tabNameView.setText(info.name);
                }
            }
            if (selected) {
                tabImageView.setImageBitmap(info.selectedBitmap);
            } else {
                tabImageView.setImageBitmap(info.defaultBitmap);
            }
        }
    }

    @ColorInt
    private int getColor(Object color) {
        if (color instanceof String) {
            return Color.parseColor((String) color);
        } else {
            return (int) color;
        }
    }
}





















